//
//  SWBaseUIViewController.swift
//  BaseDemo
//
//  Created by linhan on 16/2/17.
//  Copyright © 2016年 me. All rights reserved.
//

import Foundation
import UIKit
class SWBaseViewController:UIViewController
{
    var appearedFirstTime:Bool = true
    var layoutFirstTime:Bool = true
    var rotationToPortraitOnPush:Bool = false
    
    var prefersNavigationBarHidden: Bool{
        return false
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = SWAppearanceManager.themeStyle.MajorVCBackgroundColor
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        navigationBarSetup()
        if forceRotate
        {
            attemptChangeDeviceOrientation()
        }
    }
    
    override func viewDidAppear(_ animated: Bool)
    {
        super.viewDidAppear(animated)
        //SWStatisticsManager.beginLogPageView(analyticsName)
        
        //页面push过程中手势返回会导致crash，因此push过程中将手势禁用，完成后再启用
        navigationController?.interactivePopGestureRecognizer?.isEnabled = true
    }
    
    override func viewDidDisappear(_ animated: Bool)
    {
        super.viewDidDisappear(animated)
        if navigationController == nil
        {
            //(self as? SWAPIComponent)?.cancelAllRequest()
        }
    }
    
    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        if layoutFirstTime
        {
            layoutFirstTime = false
            viewWillLayoutFirstTime()
        }
    }
    
    //横屏模式下，需要在LayoutSubviews后才能获取正确宽高
    func viewWillLayoutFirstTime()
    {
        
    }
}

extension SWBaseViewController
{
    //统计数据上每个页面的名称
    var analyticsName:String
    {
        return classForCoder.description()
    }
    
    //导航栏在父类中统一设置，子类如有不同，则在子类中重写
    func navigationBarSetup()
    {
        if let navc = navigationController as? SWBaseNavigationController
        {
            if prefersNavigationBarHidden && !navc.navigationBarHiddenFlag
            {
                navc.setNavigationBarHidden(true, animated: true)
            }
            else if !prefersNavigationBarHidden && navc.navigationBarHiddenFlag
            {
                navc.setNavigationBarHidden(false, animated: true)
            }
        }
    }
}

//控制状态栏以及转向

extension SWBaseViewController
{
    //强制转向的方向
    var forceOrientation:UIInterfaceOrientation{
        return .portrait
    }
    
    //是否强制转向
    var forceRotate:Bool{
        return false
    }
    
    //尝试强制旋转设备方向，如果目标方向与设备方向一致，则不会变化
    func attemptChangeDeviceOrientation()
    {
        let orientationUnknown = NSNumber(value: UIInterfaceOrientation.unknown.rawValue)
        UIDevice.current.setValue(orientationUnknown, forKey: "orientation")
        
        let raw = forceOrientation.rawValue
        //print("attemptChangeDeviceOrientation:", raw)
        let orientationTarget = NSNumber(value: raw)
        UIDevice.current.setValue(orientationTarget, forKey: "orientation")
    }
}

extension SWBaseViewController
{
    //方面默认朝上，如有不同，在子类中重写
    override var supportedInterfaceOrientations: UIInterfaceOrientationMask
    {
        //return childViewControllers.first?.supportedInterfaceOrientations() ?? .LandscapeLeft
        return childViewControllers.first?.supportedInterfaceOrientations ?? .portrait
    }
    
    override var shouldAutorotate: Bool
    {
        return childViewControllers.first?.shouldAutorotate ?? false
    }
    
    //控制present的VC转向
    override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation
    {
        //return childViewControllers.first?.preferredInterfaceOrientationForPresentation() ?? UIInterfaceOrientation.LandscapeLeft
        return childViewControllers.first?.preferredInterfaceOrientationForPresentation ?? UIInterfaceOrientation.portrait
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle
    {
        return childViewControllers.first?.preferredStatusBarStyle ?? UIStatusBarStyle.default
    }
    
    override var prefersStatusBarHidden: Bool
    {
        return childViewControllers.first?.prefersStatusBarHidden ?? false
    }
    
    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation
    {
        return childViewControllers.first?.preferredStatusBarUpdateAnimation ?? .fade
    }
    
    override var childViewControllerForStatusBarStyle: UIViewController?
    {
        return childViewControllers.first
    }
}
